package com.soulkun.smarthome.ui.main.personal

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainPersonalFamilyBinding
import com.soulkun.smarthome.databinding.AppRecycleItemMainPersonalFamilyFamilyListBinding
import com.soulkun.smarthome.logic.enum.NavigationEnum
import com.soulkun.smarthome.ui.main.MainNavigationDataBindingFragment
import com.soulkun.smarthome.ui.main.personal.adapter.AppMainPersonalFamilyFamilyListAdapter
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalFamilyViewModel
import soulkun.library.utils.navigateSafe
import soulkun.library.utils.screen.ScreenUtils

class AppMainPersonalFamilyFragment :
    MainNavigationDataBindingFragment<AppFragmentMainPersonalFamilyBinding>(false) {

    private val viewModel by lazy {
        ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory(mActivity.application)
        ).get(AppMainPersonalFamilyViewModel::class.java)
    }

    private val adapter by lazy {
        AppMainPersonalFamilyFamilyListAdapter(mContext, viewModel.familyList) { binding ->
            View.OnClickListener {
                findNavController().navigateSafe(
                    R.id.action_appMainPersonalFamilyFragment_to_app_navigation_main_personal_family_setting,
                    R.id.appMainPersonalFamilyFragment,
                    Bundle().apply {
                        putLong(
                            "familyId",
                            (binding as AppRecycleItemMainPersonalFamilyFamilyListBinding).familyModel!!.id!!
                        )
                    })
            }
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_personal_family
    }

    override fun initView() {
        super.initView()
        ScreenUtils.setViewFitSystemWindows(requireDataBinding().root)
        requireDataBinding().rvMainPersonalFamilyListFamily.run {
            layoutManager = LinearLayoutManager(mContext)
            adapter = this@AppMainPersonalFamilyFragment.adapter
        }
    }

    override fun initData() {
        requireDataBinding().lifecycleOwner = viewLifecycleOwner
        requireDataBinding().viewModel = viewModel
        requireDataBinding().onClickListener = View.OnClickListener {
            when (it.id) {
                R.id.tv_main_personal_family_text_create -> {
                    findNavController().navigateSafe(
                        R.id.action_appMainPersonalFamilyFragment_to_app_navigation_family_create,
                        R.id.appMainPersonalFamilyFragment,
                        Bundle().apply {
                            putSerializable("navigationType", NavigationEnum.Main)
                        }
                    )
                }
                R.id.tv_main_personal_family_text_join -> {
                    findNavController().navigateSafe(
                        R.id.action_appMainPersonalFamilyFragment_to_appMainPersonalFamilyJoinFragment2,
                        R.id.appMainPersonalFamilyFragment,
                        Bundle().apply {
                            putSerializable("navigationType", NavigationEnum.Main)
                        }
                    )
                }
                R.id.iv_main_personal_family_icon_back -> {
                    findNavController().navigateUp()
                }
            }
        }
        viewModel.getFamilyNameList()
    }

    override fun initListener() {
    }

    override fun doOnCreate() {
    }

}